/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gene.core.collection;

import gene.core.Component;

/**
 *
 * @author vlm
 */
public class ComponentCollection {

    //private Component array[];//Component[30];
    //private short [] index_getIndex = {0,0};
    //private int getIndex = -1;
    //Component pivot = null;
    //public void setPivotComponent(Component c) {
        // array[0] = c;
    //}

    public void add(Component c) {
    System.out.println("add "+c);

        if (Component.head == null) {
            Component.head = c;
            Component.ground = c;
        } else {
            Component.ground.next = c;
            Component.ground = c;
        }

    //if(index_getIndex[0] < array.length){
    //array[ index_getIndex[0] ] = c;
    //index_getIndex[0]++;
    //}

    }

    public ComponentCollection() {
        //array = new Component[15];
    }

    public void rem(Component c) {
        //boolean arrange = false;

        /*for (int x = 0; x < this.index_getIndex[0]; x++) {
        if (array[x] == c) {
        arrange = true;
        }
        if (arrange) {
        array[x - 1] = array[x];
        }
        }*/
        //int i = 0;
        //for(i = index_getIndex[0]; --i >= 0;){
        //   if (array[i] == c) {
        //       arrange = true;
        //   }
        //   if (arrange) {
        //       array[i - 1] = array[i];
        //   }
        //}

        if (Component.head.equals(c)) {
            Component.head = Component.head.next;
        } else {

            Component tmp = Component.head.next;

            while (tmp.next != null) {
                if (tmp.next.equals(c)) {
                    tmp.next = tmp.next.next;
                }
            }
        }

    }

    public Component get(int index) {
        /*for (int x = 0; x < this.index_getIndex[0]; x++) {
        if (array[x] == c) {
        return array[x];
        }
        }*/


        /*int i = 0;
        for (i = index_getIndex[0]; --i >= 0;) {
            if (array[i] == c) {
                //System.out.println("*************");
                return array[i];
            }
        }*/
        int cont=0;

        Component tmp = Component.head;
        for(;cont <= index;cont++){
        tmp = tmp.next;
        }
        return tmp;
        
        /*
        Component tmp = Component.head;
        while(tmp!=null){
          if(tmp.equals(c)){return c;}
          tmp = tmp.next;
        }*/

        //return null;
    }
/*
    public Component get() {
        return array[index_getIndex[1]];
    }
*/
    /*public Component next() {
        index_getIndex[1]++;


        if (index_getIndex[1] > array.length) {
            index_getIndex[1] = 0;
        }

        return array[index_getIndex[1]];

    }*/

    /*
    public Component ante() {
        index_getIndex[1]--;


        if (index_getIndex[1] < 0) {
            index_getIndex[1] = (short) array.length;
        }

        return array[index_getIndex[1]];

    }*/

    public void update() {
        //System.out.println("UPDATE");
       /* for (int x = 0; x < index_getIndex[0]; x++) {
            array[x].update();
        }*/

        Component tmp = Component.head;

        while(tmp!=null){
         tmp.update();
         tmp.updateTick();
         tmp = tmp.next;
        }
        
    }

    /**
     * verifica se o componente colide com algum
     * @param component
     * @return
     */
    public void collids(){//Component component) {

        /*

        for (int x = 0; x != (array.length) && (array[x] != null); x++) {

            //testa se o ponteiro é diferente da posicao atual
            if (component != array[x] &&
                    array[x].isVisible()) {

                //testa a colisao e retorna o index caso
                if (array[x].collidsWith(component)) {
                    return x;
                }
            }
        }
        return -1;
         * */

        Component pivot = Component.head;
        Component tmp = null;
        while(pivot!=null){
            tmp=pivot.next;
            while(pivot!=null){
                tmp.collidsWith(pivot);
                tmp = tmp.next;
            }
        pivot = pivot.next;
        }

        

    }

    /*
     * verifica se o componente colide com algum
     * @param component
     * @return
     *
    public int collides(int index) {

        Component component = getComponent(index);
        collides(component);
        component = null;
        return -1;
    }*/

    /*
     * make the collision between all components , testing a.collision(b) and
     * b.collision(a)
     *
    public void collisionAll() {

        Component pivot = array[0];

        int idx = index_getIndex[0];
        int i = idx;
        //int idx = 0;
        //for (int i = idx; i != index_getIndex[0]/*this.index/*this.array.length*; i++) {
        for (; --i > 0;) {
            if (i == array.length - 1) {
                //idx++;
                --idx;
                i = idx;

                pivot = array[i];
            }

            pivot.collidsWith(array[i]);
            array[i].collidsWith(pivot);


        }
    }*/

    /**
     * draw all componets on screen buffer
     */
    public void draw() {
        //int i = index_getIndex[0];

        /*for (int x = 0; x < index_getIndex[0]; x++) {
        switch (array[x].getType()) {
        case 0: {
        array[x].draw();
        }}}*/

        //for (/*i = index_getIndex[0]*/; --i >= 0;) {
        /*    switch (array[i].getType()) {
                case 0: {
                    array[i].draw();
                }
            }
        }*/

        Component tmp = Component.head;
        while(tmp!=null){
         tmp.draw();
         tmp = tmp.next;
        }

    }

    /*public Component getComponent(int index) {
        if (index > index_getIndex[0]) {
            return array[index];
        }

        return null;
    }*/

    /**
     * set the start state, position and other variables for the collection;
     */
    public void start() {
        //int i = index_getIndex[0];

        //for (int x = 0; x < index_getIndex[0]; x++) {
        //for (; --i > 0;) {
        //    array[i].start();
        //}

        Component tmp = Component.head;
        while(tmp!=null){
         tmp.start();
         tmp = tmp.next;
        }


    }

    public void destroy() {

        //int i = index_getIndex[0];

        //for (int x = 0; x < index_getIndex[0]; x++) {
        //for (; --i > 0;) {
        //    array[i].destroy();
        //    array[i] = null;
        //}
        Component tmp = Component.head;
        while(tmp!=null){
         tmp.destroy();
         tmp = tmp.next;
        }

    }
}
